import { reactive, inject, computed } from 'vue'
// import { UserApi } from '../../../api/index.js'
import { UserApi } from '@@/api/api.js'
import { compareVersion } from '@@/utils/common.js'

function useMeInfo() {
	const state = reactive({})
	const vueState = inject('vueState')
	const needUpdate = computed(() => {
		const { version, lastestVersion } = vueState.versionControl
		if (lastestVersion === '') {
			return false
		} else if (version === lastestVersion) {
			return false
		} else {
			return compareVersion(version || '1.12.0', lastestVersion || '')
		}
	})
	const messageUnread = computed(() => {
		const unReadArr = vueState.pageInfo.messageInfo?.list.filter((item) => {
			return item.readed === false
		})
		window.SDKPostMessage({
			event: 'h5_letter_info',
			data: {
				'cnt_unread': unReadArr.length || 0
			}
		})
		return unReadArr?.length
	})
	const feedbackUnread = computed(() => {
		const unReadArr = vueState.pageInfo.feedbackInfo?.list.filter((item) => {
			return item.readed === false
		})
		return unReadArr?.length
	})
	const getFeedbackInfo = () => {
		UserApi.GetFeedBackList().then((res) => {
			const { userFeedBackList } = res
			vueState.pageInfo.feedbackInfo = { list: userFeedBackList }
		}).catch((error) => {
			console.log(error);
		})
	}
	const getMessageInfo = () => {
		UserApi.GetMessageInfo({ channel: vueState.pageInfo.channel }).then((res) => {
			const { list } = res
			vueState.pageInfo.messageInfo = { list }
		}).catch((error) => {
			console.log(error);
		})
	}
	const initPage = () => {
		getFeedbackInfo()
		getMessageInfo()
		vueState.pageInfo.timer = setTimeout(initPage, 120000);
	}
	return {
		state,
		initPage,
		needUpdate,
		getFeedbackInfo,
		getMessageInfo,
		messageUnread,
		feedbackUnread
	}
}
export { useMeInfo }